﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering.View_Operaction;
using Microsoft.Office.Core;
using System.IO;

namespace Catering.Catering_Page.Kufang
{

    public partial class Rkgl : UserIsNull
    {
        Catering_Rules.InDbInfo CR_Info = new Catering_Rules.InDbInfo();
        Catering_Model.InDbInfo CM_Info = new Catering_Model.InDbInfo();
        Catering.View_Operaction_Common voc = new View_Operaction_Common();
        DataBase_Manage_Operaction dmo = new DataBase_Manage_Operaction();
        public int m_CurrentIndex = 1;
        public string m_GroupID = "-10";
        public string m_ClassID = "-10";
        public string m_SearchType = "";
        public string m_SearchValue = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            this.txt_riqi.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy-MM-dd',maxDate:'%y-%M-%d'})");
            this.txt_addtime.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy-MM-dd',maxDate:'%y-%M-%d'})");
            m_CurrentIndex = this.AspNetPager1.CurrentPageIndex;
            if (!IsPostBack)
            {
                if (Session["User"] != null)
                {
                    string userid = Session["PersonnelID"].ToString();
                    int companyid = 0;
                    int groupid = 0;
                    int classid = 0;
                    bool isAdmin = false;
                    Bind_Data_to_Controls.GetCompanyByCurrentUser(userid, out companyid, out groupid, out classid, out isAdmin);
                    Bind_Data_to_Controls.Bind_Area_ddl(DDL_zuoyequ, isAdmin, groupid, companyid, true, false);
                    //Bind_Data_to_Controls.Bind_Class_ddl(DDL_zuoyequ.SelectedItem, DDL_pingtai, isAdmin, classid, false);
                    #region 初始化参数

                    if (Request.QueryString["CurrentPage"] != null)
                    {
                        Int32.TryParse(Request.QueryString["CurrentPage"].ToString(), out m_CurrentIndex);
                    }

                    if (Request.QueryString["GroupID"] != null)
                    {
                        m_GroupID = Request.QueryString["GroupID"].ToString();
                    }

                    if (Request.QueryString["ClassID"] != null)
                    {
                        m_ClassID = Request.QueryString["ClassID"].ToString();
                    }

                    if (Request.QueryString["SearchType"] != null)
                    {
                        m_SearchType = Request.QueryString["SearchType"].ToString();
                    }

                    if (Request.QueryString["SearchValue"] != null)
                    {
                        m_SearchValue = Request.QueryString["SearchValue"].ToString();
                    }

                    DDL_zuoyequ.SelectedValue = m_GroupID;
                    Bind_Data_to_Controls.Bind_Class_ddl(DDL_zuoyequ.SelectedItem, DDL_pingtai, isAdmin, classid, true);
                    DDL_pingtai.SelectedValue = m_ClassID;
                    DDL_anzhao.SelectedValue = m_SearchType;
                    TB_chaxun.Text = m_SearchValue;
                    this.txt_addtime.Text = DateTime.Now.ToString("yyyy-MM-dd");

                    #endregion
                }



                //this.RepBing("1=1", " order by IntDate desc");
                this.BT_chaxun_Click(null, null);
            }
        }

        //根据条件进行查询
        protected void BT_chaxun_Click(object sender, EventArgs e)
        {
            ViewState["IsSearch"] = "true";
            int pagecurrent = m_CurrentIndex;
            if (sender != null) pagecurrent = 1;
            this.RepBing(GetStrWhere(), pagecurrent, " order by IntDate desc");
        }

        //获取查询条件
        public string GetStrWhere()
        {
            string strWhere = " 1=1 ";
            if (!string.IsNullOrEmpty(this.DDL_zuoyequ.SelectedValue) && this.DDL_zuoyequ.SelectedValue != "-10" && this.DDL_zuoyequ.SelectedValue != "-1") strWhere += " and GroupID=" + this.DDL_zuoyequ.SelectedValue;
            if (!string.IsNullOrEmpty(this.DDL_pingtai.SelectedValue) && this.DDL_pingtai.SelectedValue != "-10" && this.DDL_pingtai.SelectedValue != "-1") strWhere += " and ClassID=" + this.DDL_pingtai.SelectedValue;
            switch (this.DDL_anzhao.SelectedValue)
            {
                case "Raw":
                    strWhere = strWhere + " and Raw like '%" + TB_chaxun.Text.Trim() + "%'";
                    break;
                case "IntDate":
                    if (TB_chaxun.Text.Trim() != "")
                    {
                        strWhere = strWhere + " and IntDate>=convert(datetime,'" + Convert.ToDateTime(TB_chaxun.Text.Trim()).ToShortDateString() + "') and IntDate<convert(datetime,'" + Convert.ToDateTime(TB_chaxun.Text.Trim()).AddDays(1).ToShortDateString() + "')";
                    }
                    break;
                case "InDBId":
                    if (TB_chaxun.Text.Trim() != "")
                    {
                        strWhere = strWhere + " and InDBId='" + TB_chaxun.Text.Trim() + "'";
                    }
                    break;
            }
            //strWhere += " order by IntDate desc";
            return strWhere;
        }

        //绑定Rep控件
        public void RepBing(string strwhere, int pagecurrent, string strorder = "")
        {
            this.AspNetPager1.RecordCount = new Catering_Rules.InDbInfo().GetCountBywhere(strwhere);
            this.AspNetPager1.CurrentPageIndex = pagecurrent;
            #region 设置参数

            m_CurrentIndex = pagecurrent;
            m_ClassID = this.DDL_pingtai.SelectedValue;
            m_GroupID = this.DDL_zuoyequ.SelectedValue;
            m_SearchType = this.DDL_anzhao.SelectedValue;
            m_SearchValue = this.TB_chaxun.Text;

            #endregion

            this.Repeater1.DataSource = new Catering_Rules.InDbInfo().GetList(this.AspNetPager1.PageSize, pagecurrent - 1, strwhere + strorder);
            this.Repeater1.DataBind();
        }

        //获取虚列索引
        public int i = 0;
        public string GetindexXu()
        {
            i += 1;
            return ((this.AspNetPager1.CurrentPageIndex - 1) * this.AspNetPager1.PageSize + i).ToString();
        }

        // 变更查询条件
        protected void DDL_anzhao_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.TB_chaxun.Text = "";
            if (DDL_anzhao.SelectedIndex > 1)
            {
                this.TB_chaxun.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy-MM-dd',maxDate:'%y-%M-%d'})");
            }
            else
            {
                this.TB_chaxun.Attributes.Clear();
            }

        }

        //导出
        protected void BT_daochu_Click(object sender, EventArgs e)
        {
            DataTable dt = new Catering_Rules.InDbInfo().GetList(GetStrWhere() + " order by IntDate desc ").Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                Microsoft.Office.Interop.Excel.Application excel1 = null;
                Microsoft.Office.Interop.Excel._Worksheet ws = null;
                Microsoft.Office.Interop.Excel.Range canbie = null;
                string stick = DateTime.Now.Ticks.ToString();

                try
                {
                    excel1 = new Microsoft.Office.Interop.Excel.Application();
                    excel1.Application.Workbooks.Add(true);//引用Excel工作簿
                    //Microsoft.Office.Interop.Excel._Worksheet ws = new Microsoft.Office.Interop.Excel.WorksheetClass();
                    ws = new Microsoft.Office.Interop.Excel.WorksheetClass();
                    ws = (Microsoft.Office.Interop.Excel._Worksheet)excel1.ActiveSheet;//获取当前活动工作薄
                    //Microsoft.Office.Interop.Excel.Range canbie;//创建Range操作一行作为标题
                    excel1.Visible = false;

                    #region 第一行标题

                    ws.Cells[1, 1] = "序号";
                    ws.Cells[1, 2] = "品号";
                    ws.Cells[1, 3] = "入库日期";
                    ws.Cells[1, 4] = "单据日期";
                    ws.Cells[1, 5] = "品名";
                    ws.Cells[1, 6] = "规格";
                    ws.Cells[1, 7] = "入库数量";
                    ws.Cells[1, 8] = "单位";
                    ws.Cells[1, 9] = "单价";
                    ws.Cells[1, 10] = "总金额";
                    ws.Cells[1, 11] = "备注";
                    canbie = ws.get_Range(ws.Cells[1, 1], ws.Cells[1, 11]);
                    canbie.HorizontalAlignment = XlVAlign.xlVAlignCenter;//居中

                    #endregion

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        //select id,GroupID,ClassID,DBID,IntDate,PiaoDate,RAWID,RAW,Specifications,Number,CMP,AmountMoney,Weight,RAWTID,RAWIDS,Back,temp1,temp2,InDBid,Price,SumNumber 
                        string InDBid = dt.Rows[i]["InDBid"].ToString();
                        string intDate = Convert.ToDateTime(dt.Rows[i]["IntDate"]).ToString("yyyy/MM/dd");
                        string piaoju = Convert.ToDateTime(dt.Rows[i]["PiaoDate"]).ToString("yyyy/MM/dd");
                        string raw = dt.Rows[i]["RAW"].ToString();
                        string specifications = dt.Rows[i]["Specifications"].ToString();
                        string number = dt.Rows[i]["Number"].ToString();
                        string CMP = dt.Rows[i]["CMP"].ToString();
                        string price = dt.Rows[i]["Price"].ToString();
                        string weight = Catering.Toolss.GetMathResult(Convert.ToDecimal(number) * Convert.ToDecimal(price), 2, 3).ToString();
                        string back = dt.Rows[i]["Back"].ToString();
                        ws.Cells[2 + i, 1] = (i + 1);
                        ws.Cells[2 + i, 2] = InDBid;
                        ws.Cells[2 + i, 3] = intDate;
                        ws.Cells[2 + i, 4] = piaoju;
                        ws.Cells[2 + i, 5] = raw;
                        ws.Cells[2 + i, 6] = specifications;
                        ws.Cells[2 + i, 7] = number;
                        ws.Cells[2 + i, 8] = CMP;
                        ws.Cells[2 + i, 9] = price;
                        ws.Cells[2 + i, 10] = weight;
                        ws.Cells[2 + i, 11] = back;
                    }
                    ws.Cells.EntireColumn.AutoFit();
                    canbie = ws.get_Range(ws.Cells[1, 1], ws.Cells[dt.Rows.Count + 1, 11]);
                    canbie.Borders.LineStyle = 1;
                    dt.Dispose();

                    if (!Directory.Exists(Server.MapPath("../../xls_template"))) Directory.CreateDirectory(Server.MapPath("../../xls_template"));
                    ws.SaveAs(Server.MapPath("../../xls_template") + "\\" + stick + ".xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlAddIn8);
                }
                finally
                {
                    if (excel1 != null)
                    {
                        if (ws != null)
                        {
                            if (canbie != null)
                            {
                                System.Runtime.InteropServices.Marshal.ReleaseComObject(canbie);
                                canbie = null;

                            }
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
                            ws = null;
                        }
                        excel1.Application.Workbooks.Close();
                        excel1.Quit();
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(excel1);
                        Toolss.KillExcel(excel1);
                        excel1 = null;
                        GC.Collect();
                    }
                }

                #region 发送

                try
                {
                    string path = Server.MapPath("../../xls_template") + "\\" + stick + ".xls";
                    System.IO.FileInfo file = new System.IO.FileInfo(path);
                    Response.Clear();
                    Response.Charset = "GB2312";
                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    // 添加头信息，,弹出另存为窗口  
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
                    //让浏览器头显示下载信息  
                    Response.AddHeader("Content-Length", file.Length.ToString());
                    // 指定返回一个不能被客户端读取的流，下载  
                    Response.ContentType = "application/ms-excel";
                    //把文件流下载到客户端  
                    Response.WriteFile(file.FullName);
                    Response.Flush();
                    // 停止页面的执行
                    Response.End();

                #endregion

                }
                catch
                {
                }
            }
            else
            {
                View_Operaction_Common.Show(this, "没有可导出的数据");
            }
        }

        //添加数据
        protected void but_Insert_Indata_Click(object sender, EventArgs e)
        {
            Catering_Model.InDbInfo cmii = new Catering_Model.InDbInfo();
            cmii.id = new Catering_Rules.InDbInfo().GetMaxId();
            cmii.GroupID = Convert.ToInt32(this.DDL_zuoyequ.SelectedValue);
            cmii.ClassID = Convert.ToInt32(this.DDL_pingtai.SelectedValue);
            cmii.DBID = 1;
            cmii.RAWID = Convert.ToInt32(this.HidRawID.Value);
            DateTime AddDate = DateTime.Now;
            DateTime.TryParse(this.txt_addtime.Text, out AddDate);
            cmii.IntDate = AddDate;
            cmii.PiaoDate = Convert.ToDateTime(this.txt_riqi.Text.Trim());
            cmii.RAW = this.txt_Raw_Name.Text.Trim();
            cmii.Specifications = this.txt_guige.Text.Trim();
            //cmii.Number = this.txt_in_Number.Text.Trim();
            //cmii.CMP = this.Txt_danwei.SelectedItem.Text;
            cmii.AmountMoney = Convert.ToDecimal(this.txt_jine.Text.Trim());
            cmii.Back = this.txt_back.Text.Trim();
            cmii.InDBid = this.Txt_Raw_No.Text.Trim();
            //cmii.Price = this.txt_danjia.Text.Trim();

            decimal number = 0;
            decimal price = 0;
            Catering_Model.E_RawMaterial md_Raw = new Catering_Rules.T_RawMaterial().GetModel(Convert.ToInt32(cmii.RAWID));
            cmii.RAWTID = md_Raw.RAWID;
            cmii.RAWIDS = md_Raw.RAWIDS;
            cmii.InDBid = this.Txt_Raw_No.Text.Trim();
            Decimal.TryParse(this.txt_in_Number.Text.Trim(), out number);
            Decimal.TryParse(this.txt_danjia.Text.Trim(), out price);
            string unit = new Catering_Rules.Units().GetModel(Convert.ToInt32(md_Raw.temp2)).UName;
            string currentunit = this.danwei.Value;
            if (unit != currentunit)
            {
                DataTable dt_RMUnit1 = new Catering_Rules.RawMaterial_Unit().GetUnitByRawMaterialID(md_Raw.id).Tables[0];

                List<decimal> danweiguige = new List<decimal>();
                List<string> danweiguigeName = new List<string>();
                int biaozhun = 0;
                int current = 0;
                if (dt_RMUnit1 != null && dt_RMUnit1.Rows.Count > 0)
                {
                    for (int i = 0; i < dt_RMUnit1.Rows.Count; i++)
                    {
                        decimal tempnum = Convert.ToDecimal(dt_RMUnit1.Rows[i]["Number"]);
                        string danwei = dt_RMUnit1.Rows[i]["UName"].ToString();
                        if (danwei == unit) biaozhun = i;
                        if (danwei == currentunit) current = i;
                        danweiguige.Add(tempnum);
                        danweiguigeName.Add(danwei);
                    }
                    dt_RMUnit1.Dispose();
                    decimal fun = 1;
                    if (biaozhun > current)
                    {
                        for (int i = (current + 1); i <= biaozhun; i++)
                        {
                            fun = danweiguige[i] * fun;
                        }
                        number = Toolss.GetMathResult((number * fun), 2, 3);
                        price = Toolss.GetMathResult((price / fun), 2, 3);
                    }
                    else if (biaozhun < current)
                    {
                        for (int i = current; i > biaozhun; i--)
                        {
                            fun = danweiguige[i] * fun;
                        }
                        number = Toolss.GetMathResult((number / fun), 2, 3);
                        price = Toolss.GetMathResult((price * fun), 2, 3);
                    }
                }
            }
            cmii.Number = number.ToString();
            cmii.CMP = unit;
            cmii.Price = price.ToString();
            cmii.temp1 = "0";
            cmii.temp2 = "0";

            decimal sum = 0;//总重量
            Decimal.TryParse(cmii.Number, out sum);
            bool iskg = true;
            decimal guige = 1;
            decimal UtilKg = Toolss.GetSpecifications(Convert.ToInt32(cmii.RAWID), unit, out iskg);
            guige = iskg ? 1000 : 1;
            decimal weight = Toolss.GetMathResult(UtilKg * sum, 2, 3);

            cmii.Weight = weight.ToString();
            cmii.SumNumber = weight * guige;

            #region 入库判断

            ArrayList cal = new Catering_Rules.InDbInfo().CanbeInDBInfo(cmii);
            if (cal != null && cal[0].ToString() != "OK")
            {
                View_Operaction_Common.Show(this, cal[1].ToString());
                return;
            }

            #endregion

            new Catering_Rules.BalanceInfo().AddInDBInfo(Convert.ToInt32(this.HidRawID.Value), Convert.ToInt32(this.DDL_pingtai.SelectedValue), cmii.InDBid, AddDate, cmii.Number, cmii.Price, cmii.id);
            new Catering_Rules.InDbInfo().Add(cmii);
            View_Operaction_Common.Show(this, "添加成功");
            this.ClearAddDiv();
            this.BT_chaxun_Click(null, null);
        }

        //清空控件
        public void ClearAddDiv()
        {
            foreach (Control c in this.Pan_Add_div.Controls)
            {
                TextBox tb = c as TextBox;
                if (tb != null)
                {
                    tb.Text = string.Empty;
                }
            }
        }

        //作业区以及工作组
        protected void DDL_zuoyequ_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind_Data_to_Controls.Bind_Class_ddl(DDL_zuoyequ.SelectedItem, DDL_pingtai, Base_IsAdmin, 0, true);
            //voc.DropDownListDataBind_Class(this.DDL_pingtai, DDL_zuoyequ.SelectedValue);
        }

        //删除
        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                if (Session["ExcelInfo"] != null)
                {
                    DataTable dt_temp = Session["ExcelInfo"] as DataTable;
                    int i = Convert.ToInt32(e.CommandArgument.ToString());
                    dt_temp.Rows.RemoveAt(i - 1);
                    Session["ExcelInfo"] = dt_temp;
                    Response.Redirect("Rkgl.aspx?CurrentPage=" + m_CurrentIndex + "&GroupID=" + m_GroupID + "&ClassID=" + m_ClassID + "&SearchType=" + m_SearchType + "&SearchValue=" + m_SearchValue);
                }
                else
                {
                    int result = new Catering_Rules.BalanceInfo().DelInDBInfo(Convert.ToInt32(e.CommandArgument.ToString()));
                    if (result == 3)
                    {
                        new Catering_Rules.InDbInfo().Delete(Convert.ToInt32(e.CommandArgument.ToString()));
                        View_Operaction_Common.Show(this, "删除成功");
                    }
                    else if (result == 2)
                    {
                        View_Operaction_Common.Show(this, "已出库，结余不足，删除失败");
                    }
                    else
                    {
                        View_Operaction_Common.Show(this, "操作失误，删除失败");
                    }
                    this.BT_chaxun_Click(null, null);
                }
            }
        }

        //根据品号加载原料数据
        [WebMethod]
        public static ArrayList InputRoomRawID_Chanage(string strID)
        {
            ArrayList list = new ArrayList();
            list = GetDataBaseNumber(strID);
            return list;
        }

        public static ArrayList GetDataBaseNumber(string strInfo)
        {
            ArrayList list = new ArrayList();
            string[] infos = strInfo.Split(',');

            DataTable dt_RawMaterial_Area = new Catering_Rules.RawMaterial_Area().GetList("InDBid='" + infos[2] + "'").Tables[0];
            if (dt_RawMaterial_Area != null && dt_RawMaterial_Area.Rows.Count > 0)
            {
                int RawID = Convert.ToInt32(dt_RawMaterial_Area.Rows[0]["RawID"].ToString());
                Catering_Model.E_RawMaterial md_Raw = new Catering_Rules.T_RawMaterial().GetModel(RawID);
                if (md_Raw != null)
                {
                    list.Add("OK");
                    //名称
                    list.Add(md_Raw.RAWName);
                    //ArrayList al = new Catering_Rules.BalanceInfo().GetLastBalanceInfo(infos[1], RawID.ToString(), infos[2]);
                    //if (al != null && al.Count >= 3)
                    //{
                    //    list.Add(Toolss.GetMathResult(Convert.ToDecimal(al[1]), 2, 3));// 结余量
                    //    list.Add(Toolss.GetMathResult(Convert.ToDecimal(al[2]), 2, 3));//单价                        
                    //}
                    //else
                    //{
                    //    list.Add("");//结余量
                    //    list.Add("");//单价
                    //}

                    //入库单位
                    string unit = new Catering_Rules.Units().GetModel(Convert.ToInt32(md_Raw.temp2)).UName;
                    decimal UtilKg = 0;
                    list.Add(unit);

                    //规格
                    list.Add(GetSpecifications(RawID.ToString(), unit, out UtilKg));

                    //原料ID
                    list.Add(RawID.ToString());

                    //重量计算
                    list.Add(UtilKg);

                    //获取规格
                    DataTable dt_RMUnit = new Catering_Rules.RawMaterial_Unit().GetUnitByRawMaterialID(md_Raw.id).Tables[0];
                    string strunit = "";
                    if (dt_RMUnit != null && dt_RMUnit.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt_RMUnit.Rows.Count; i++)
                        {
                            decimal tempnum = Convert.ToDecimal(dt_RMUnit.Rows[i]["Number"]);
                            string danwei = dt_RMUnit.Rows[i]["UName"].ToString();
                            if (danwei != unit)
                            {
                                strunit += "<option value='" + danwei + "'>" + danwei + "</option>";
                            }
                            else
                            {
                                strunit += "<option selected='selected' value='" + danwei + "'>" + danwei + "</option>";
                            }
                        }
                        dt_RMUnit.Dispose();
                    }
                    list.Add(strunit);

                }
            }
            else
            {
                list.Add("NO");
            }
            return list;
        }

        //获取原料规格
        public static string GetSpecifications(string strRawID, string UtilName, out decimal UtilKg)
        {
            string str_Specifications = "";
            UtilKg = 1;
            bool flag = true;
            DataTable dt_RawUnit = new Catering_Rules.RawMaterial_Unit().GetList("RawMaterialID=" + strRawID).Tables[0];
            if (dt_RawUnit != null && dt_RawUnit.Rows.Count > 0)
            {
                foreach (DataRow dr in dt_RawUnit.Rows)
                {
                    string number = dr["Number"].ToString();
                    if (!string.IsNullOrEmpty(number))
                    {
                        Catering_Model.Units md_unti = new Catering_Rules.Units().GetModel(Convert.ToInt32(dr["UnitID"]));
                        str_Specifications = str_Specifications + dr["Number"] + md_unti.UName + "|";
                        if (flag && md_unti.UName == "kg") flag = false;
                        if (flag) UtilKg = UtilKg * Convert.ToDecimal(number);

                    }
                }
            }
            return str_Specifications.TrimEnd('|');
        }

        //分页
        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            if (ViewState["IsSearch"] != null)
            {
                this.RepBing(GetStrWhere(), e.NewPageIndex, " order by IntDate desc");
            }
            else
            {
                this.RepBing("1=1", e.NewPageIndex, " order by IntDate desc");
            }
        }

        public string subPrice(string Price)
        {
            if (!string.IsNullOrEmpty(Price))
            {
                if (Price.Length >= 6)
                {
                    Price = Convert.ToDouble(Price).ToString("F2");
                }
            }
            return Price;
        }

        public string GetUnitByRawID(string StrRawid)
        {
            int id = 0;
            Int32.TryParse(StrRawid, out id);
            if (id > 0)
            {
                Catering_Model.E_RawMaterial model = new Catering_Rules.T_RawMaterial().GetModel(id);
                if (model != null)
                {
                    Catering_Model.Units unit = new Catering_Rules.Units().GetModel(Convert.ToInt32(model.temp2));
                    if (unit != null) return unit.UName;
                }
            }
            return "";
        }
    }
}